// 租赁合同
import url from "../../../config/url";
import axios from "axios";
axios.defaults.baseURL = url.apiUrl;
import listApi from "../../../api/enterprise";
const enterprisecontract = {
  namespaced: true,
  state: {
    companyName: "", //公司id
    companyOptions: [], //公司数据项
    contractInfo: {}, //
    form: [],
    checked: false, //是否同意本合同复选框
    isShow: true,
    updateContractStatus: {
      text: ""
    }
  },

  mutations: {
    mutCompanyName(state, payload) {
      state.companyName = payload;
    },
    mutContractInfo(state, payload) {
      state.contractInfo = payload;
    },
    mutForm(state, paylaod) {
      state.form = paylaod;
    },
    mutIsShow(state, payload) {
      state.isShow = payload;
    }
  },

  actions: {
    actList(ctx) {
      //获取公司
      return axios
        .get(listApi.checkContracts, {})
        .then(response => {
          if (response.data.data == null || response.data.data == undefined) {
            ctx.state.updateContractStatus.text = response.data.message;
            ctx.state.isShow = false;
            return null;
          }
          ctx.state.isShow = false;
          ctx.state.companyOptions = response.data.data.map(item => {
            return {
              text: item.bCorpName,
              value: item.contractId
            };
          });
          ctx.state.companyName = ctx.state.companyOptions[0].value || "";
        })
        .catch(error => {
          ctx.state.isShow = false;
          ctx.state.updateContractStatus.text = error.message;
        });
    },
    actContract(ctx) {
      //获取公司合同详情
      return axios
        .get(listApi.getContract + "?id=" + ctx.state.companyName, {})
        .then(response => {
          let len = Object.keys(response.data.data).length;

          if (len > 0) {
            let a = {};
            for (let key in response.data.data) {
              ctx.state.contractInfo[key] =
                response.data.data[key] == null ? "" : response.data.data[key];
              a[key] =
                response.data.data[key] == null ? "" : response.data.data[key];
            }
          } else {
            ctx.state.contractInfo = response.data.data || {};
          }
          ctx.state.isShow = false;
        })
        .catch(error => {
          ctx.state.isShow = false;
          ctx.state.updateContractStatus.text = error.message;
        });
    },
    actSubmitContract(state) {
      //确认合同信息
      return axios
        .get(listApi.qiyeContract + "?contractId=" + ctx.state.companyName, {})
        .then(response => {
          ctx.state.updateContractStatus.text = response.data.message;
        })
        .catch(error => {
          ctx.state.updateContractStatus.text = error.message;
        });
    }
  },
  getters: {
    getCompanyName(state) {
      return state.companyName;
    },
    getCompanyOptions(state) {
      return state.companyOptions;
    },
    getContractInfo(state) {
      return state.contractInfo;
    },
    getChecked(state) {
      return state.checked;
    },
    getForm(state) {
      return state.form;
    },
    getIsShow(state) {
      return state.isShow;
    }
  }
};

export default enterprisecontract;
